package org.dragonnova.meetingapp.app.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.xys.libzxing.zxing.activity.CaptureActivity;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.utils.LogUtil;
import org.dragonnova.meetingapp.utils.ToastUtil;
import org.dragonnova.meetingapp.utils.WifiAutoConnectUtil;

import java.util.ArrayList;
import java.util.Arrays;

public class SetWifiActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = SetWifiActivity.class.getSimpleName();
    private ArrayList<String> mMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_wifi);
        initView();
        initData();
    }

    private void initData() {

    }

    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            TextView setwifi_tb_tv_title = (TextView) toolbar.findViewById(R.id.setwifi_tb_tv_title);
            setwifi_tb_tv_title.setText("会议系统网络设置");
        }

        Button setwifi_bt_scan = (Button) findViewById(R.id.setwifi_bt_scan);
        Button setwifi_bt_skip = (Button) findViewById(R.id.setwifi_bt_skip);

        setwifi_bt_scan.setOnClickListener(this);
        setwifi_bt_skip.setOnClickListener(this);
    }

    private static final int MY_PERMISSIONS_REQUEST_CAMERA = 2;

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.setwifi_bt_scan:
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.CAMERA},
                            MY_PERMISSIONS_REQUEST_CAMERA);
                } else {
                    Intent openCameraIntent = new Intent(this, CaptureActivity.class);
                    startActivityForResult(openCameraIntent, 0);
                }
                break;
            case R.id.setwifi_bt_skip:
                Intent intent = new Intent(this, MainActivity1.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == MY_PERMISSIONS_REQUEST_CAMERA) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent openCameraIntent = new Intent(this, CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
            } else {
                // Permission Denied
                ToastUtil.showTip("授权失败");
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            if (!TextUtils.isEmpty(scanResult)) {
                String[] msg = scanResult.split(" ");
                if (msg.length == 2) {
                    mMessage = new ArrayList<>(Arrays.asList(msg));
                    WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
                    new WifiAutoConnectUtil(wifiManager).connect(mMessage.get(0), mMessage.get(1), WifiAutoConnectUtil.WifiCipherType.WIFICIPHER_WPA);
                    LogUtil.d(TAG, mMessage.toString());
                } else {
                    Toast.makeText(this, getString(R.string.prompt_wrong_qr_msg), Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, getString(R.string.prompt_wrong_qr_msg), Toast.LENGTH_SHORT).show();
            }
        }
    }
}
